/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */

package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.UserDisableException;
import com.huibur.devicecheck.exception.UserInfoExistException;
import com.huibur.devicecheck.exception.UserNotFoundException;
import com.huibur.devicecheck.exception.UserNotPasswordException;
import com.huibur.devicecheck.model.param.LoginParam;
import com.huibur.devicecheck.model.param.UserParam;
import com.huibur.devicecheck.model.query.PwdQuery;
import com.huibur.devicecheck.model.query.UserQuery;
import com.huibur.devicecheck.model.vo.UserVO;
import com.huibur.devicecheck.request.ListRes;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/*******************************************************
* Title: UserController.java
* Description:
* @author baiqingshuo
* @date 2021/9/23
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/users")
@Api(value = "用户管理",tags = {"用户管理"})
@Slf4j
public class UserController {

    @Autowired
    IUserService iUserService;

    /**
     * 登录
     *
     * @param loginParam
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录", response = ObjRes.class)
    public ObjRes<UserVO> login(@RequestBody @Valid LoginParam loginParam) {
        try {
            return iUserService.login(loginParam);
        } catch (UserNotFoundException e) { //用户不存在
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.USER_NOT_FOUND.getCode(), ResultCodeEnum.USER_NOT_FOUND.getLabel());
        } catch (UserNotPasswordException e) { //用户密码错误
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.USER_NOT_PASSWORD.getCode(), ResultCodeEnum.USER_NOT_PASSWORD.getLabel());
        } catch (UserDisableException e) { //用户被禁用
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.USER_DISABLE.getCode(), ResultCodeEnum.USER_DISABLE.getLabel());
        }
    }

    /**
     * 新增用户
     *
     * @param userParam
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增用户", response = ObjRes.class)
    public ObjRes<Void> insert(@RequestBody @Valid UserParam userParam) {
        try {
            return iUserService.insert(userParam);
        } catch (UserInfoExistException e) {
            log.error(e.getMessage());
            return new ObjRes<Void>(ResultCodeEnum.USER_INFO_EXIST.getCode(), ResultCodeEnum.USER_INFO_EXIST.getLabel());
        }

    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户", response = ObjRes.class)
    public ObjRes<Boolean> deleteById(@PathVariable Long id) {
        return new ObjRes<Boolean>().data(iUserService.deleteById(id));
    }

    /**
     * 编辑用户
     *
     * @param adminId
     * @param userParam
     * @return
     */
    @PutMapping("/{adminId}")
    @ApiOperation(value = "编辑用户", response = ObjRes.class)
    public ObjRes<Void> updateUser(@PathVariable Long adminId, @RequestBody @Valid UserParam userParam) {
        try {
            return iUserService.updateUser(adminId, userParam);
        } catch (UserInfoExistException e) {
            log.error(e.getMessage());
            return new ObjRes<Void>(ResultCodeEnum.USER_INFO_EXIST.getCode(), ResultCodeEnum.USER_INFO_EXIST.getLabel());
        }

    }

    /**
     * 用户列表(查询并分页)
     *
     * @param userQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "用户列表(查询并分页)", response = PageRes.class)
    public PageRes<UserVO> selectListUser(UserQuery userQuery) {
        return iUserService.selectListUser(userQuery);
    }

    /**
     * 获取用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取用户详情", response = ObjRes.class)
    public ObjRes<UserVO> selectUserById(@PathVariable Long id) {
        return new ObjRes<UserVO>().data(iUserService.selectUserById(id));
    }

    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @PutMapping("/resetPwd")
    @ApiOperation(value = "重置密码", response = ObjRes.class)
    public ObjRes<Boolean> resetPwd(@RequestParam Long id) {
        return new ObjRes<Boolean>().data(iUserService.resetPwd(id));
    }

    /**
     * 修改密码
     *
     * @param pwdQuery
     * @return
     */
    @PutMapping("/updatePwd")
    @ApiOperation(value = "修改密码", response = ObjRes.class)
    public ObjRes<Void> updatePwd(@RequestBody PwdQuery pwdQuery) {
        return iUserService.updatePwd(pwdQuery);
    }

    /**
     * 分配时只获取巡检员
     * @return
     */
    @GetMapping("/selectCheckRole")
    @ApiOperation(value = "分配时只获取巡检员",response = ListRes.class)
    public ListRes<UserVO> selectCheckRole() {
        return new ListRes<UserVO>().data(iUserService.selectCheckRole());
    }
}
